#include "cTime_t.h"
#include <time.h>
#include <iostream>
#include <iomanip>
using namespace std;

// Initialize static fields
int cTime_t::printFormat = 1;

cTime_t::cTime_t() {
	time_t timeDate;
	struct tm* currentTime;

	timeDate = time(0);
	currentTime = localtime(&timeDate);

	updateTime(currentTime->tm_hour, currentTime->tm_min, currentTime->tm_sec);
}

cTime_t::cTime_t(const int hour, const int minutes, const int seconds) {
	updateTime(hour, minutes, seconds);
}

//TODO: check printing format, fixed size
void cTime_t::print() const {
	switch (printFormat) {
	case 1:
		cout << setw(2) << setfill('0') << hour << ":" << setw(2)
				<< setfill('0') << minutes << ":" << setw(2) << setfill('0')
				<< seconds << endl;
		break;
	case 2:
		cout << setw(2) << setfill('0') <<(hour > 12 ? hour - 12 : hour) << ":"
				<< setw(2) << setfill('0') << minutes << ":" << setw(2)
				<< setfill('0') << seconds << (hour >= 12 ? " PM" : " AM")
				<< endl;
		break;
	}
}

int cTime_t::getHour() const {
	return hour;
}

int cTime_t::getMinutes() const {
	return minutes;
}

int cTime_t::getSeconds() const {
	return seconds;
}

void cTime_t::setTime(const int hour, const int minutes, const int seconds) {
	updateTime(hour, minutes, seconds);
}

const cTime_t cTime_t::operator+(const cTime_t& ct) {
	int newSeconds = this->seconds + ct.seconds;
	int newMinutes = this->minutes + ct.minutes;
	int newHour = this->hour + ct.hour;

	updateTime(newHour, newMinutes, newSeconds);

	return *this;
}

/* Private Functions */
void cTime_t::updateTime(const int hour, const int minutes, const int seconds) {
	//TODO: bonus

	int carry=0;
	this->seconds = seconds;
	if (this->seconds >= 60) {
		carry = this->seconds / 60;
		this->seconds %= 60;
	}

	this->minutes = minutes + carry;
	carry = 0;
	if (this->minutes >= 60) {
		carry = this->minutes / 60;
		this->minutes %= 60;
	}

	this->hour = hour + carry;
	carry = 0;
	if (this->hour >= 24) {
		carry = this->hour / 24;
		this->hour %= 24;
	}
}